package com.enjoyor.soa.traffic.rest.tsms.dubbo.invoke;

import java.util.ArrayList;
import java.util.List;

import com.enjoyor.soa.traffic.api.ubms.service.dubbo.DubboRegionInfoService;
import org.springframework.stereotype.Component;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.enjoyor.soa.traffic.api.ubms.dto.DeptInfoDto;
import com.enjoyor.soa.traffic.api.ubms.dto.PersonInfoDto;
import com.enjoyor.soa.traffic.api.ubms.service.dubbo.DubboDeptInfoService;
import com.enjoyor.soa.traffic.api.ubms.service.dubbo.DubboPersonInfoService;
import com.enjoyor.soa.traffic.util.helper.ResultHelper;
import com.enjoyor.soa.traffic.util.pojo.Page;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;
import com.enjoyor.soa.traffic.util.pojo.TreeNode;

@Component
public class UbmsInfoServiceInvoke {
	/**
	 * 部门树Tree
	 */
	public static List<TreeNode> deptInfoTree;

	@Reference(timeout = 10000)
	private DubboDeptInfoService deptInfoService;

	@Reference(timeout = 10000)
	private DubboRegionInfoService dubboRegionInfoService;
	
	@Reference(timeout = 10000)
	private DubboPersonInfoService dubboPersonInfoService;

	@SuppressWarnings({ "unchecked", "static-access" })
	public List<TreeNode> getDeptTree(String token,String deptId){
		ResultPojo treeDeptInfo = deptInfoService.getTreeDeptInfo(token,deptId);
		List<TreeNode> treeNodeList = (List<TreeNode>)treeDeptInfo.getResultList();
		this.deptInfoTree = treeNodeList;
		List<TreeNode> object = new ArrayList<TreeNode>();
		for (TreeNode treeNode : treeNodeList) {
			String sourceStr = JSON.toJSONString(treeNode, SerializerFeature.DisableCircularReferenceDetect);
			//前台部门名称字段名需要命名为label
			//String replaceAll = sourceStr.replaceAll("text", "label");
			TreeNode parseObject = JSON.parseObject(sourceStr, TreeNode.class);
			object.add(parseObject);
		}
		return  object;
	}

	
	/**
	 * 递归获取部门树(速度太慢，暂时不用)
	 * 
	 * @param deptId
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "unused" })
	public List<TreeNode> getDeptInfoTree(String deptId) {
		try {
			//根据Id获取子部门
			ResultPojo DeptGroupPojo = deptInfoService.getDeptGroup(deptId);
			//返回所有部门信息
			//ResultPojo DeptInfoPojo = deptInfoService.getDeptInfo();
			//返回部门树信息
			//ResultPojo TreeDeptInfoPojo = deptInfoService.getTreeDeptInfo("a312282d-7a96-47dd-8c46-f2d1b6c6034e",deptId);

			List<DeptInfoDto> deptGroupList = (List<DeptInfoDto>) DeptGroupPojo.getResultList();
			List<TreeNode> childrenTreeNode = new ArrayList<TreeNode>();
			//有子部门-->循环查询子部门，没有子部门-->返回null
			if (deptGroupList != null && deptGroupList.size() > 0) {
				for (DeptInfoDto deptInfoDto : deptGroupList) {
					TreeNode treeDept2 = new TreeNode();
					treeDept2.setValue(deptInfoDto.getDeptId());
					treeDept2.setLabel(deptInfoDto.getDeptName());
					treeDept2.setState("open");
					treeDept2.setChecked(false);
					treeDept2.setIcon(null);
					String childDeptId = deptInfoDto.getDeptId();
					//避免循环查询父节点，陷入死循环
					if(childDeptId.equals(deptId)) {
						continue;
					}
					//递归查询
					List<TreeNode> treeChildren2 = getDeptInfoTree(childDeptId);
					if (deptGroupList != null && deptGroupList.size() > 0) {
						treeDept2.setChildren(treeChildren2);
					}
					childrenTreeNode.add(treeDept2);
				}
			} else {
				return null;
			}
			return childrenTreeNode;
		} catch (Exception e) {
			throw e;
		}
	}

	/**
	 * 返回所有行政区信息
	 * @return ResultPojo 所有行政区信息
	 */
	public  ResultPojo getRegionInfo(){
		return dubboRegionInfoService.getRegionInfo();
	}
	
	/**
	 * 获取警员信息
	 * @return ResultPojo 
	 */
	public ResultPojo getPersonInfo(String deptId, String personKey, String showNum){
		List<PersonInfoDto> resultList = new ArrayList<PersonInfoDto>();
		ResultPojo resultByDeptId = new ResultPojo();
		if(null != deptId && !"".equals(deptId)) {
			//查询该部门以及下级部门警员信息
			resultByDeptId = dubboPersonInfoService.getPersonInfoByDept(deptId);
		}else {
			//查询所有警员信息
			resultByDeptId = dubboPersonInfoService.getPersonInfo();
		}
		List<PersonInfoDto> dtoList = (List<PersonInfoDto>)resultByDeptId.getResultList();
		List<PersonInfoDto> tempList = new ArrayList<PersonInfoDto>();
		if(null != dtoList && dtoList.size() > 0) {
			if(null != personKey && !"".equals(personKey)) {
				for(PersonInfoDto personInfo : dtoList) {
					//根据警员编号、名称过滤
					if(personKey.equals(personInfo.getPersonId()) || personInfo.getPersonName().indexOf(personKey) >= 0) {
						tempList.add(personInfo);
					}
				}
			}else {
				tempList = dtoList;
			}
		}
		if(null != showNum && !"".equals(showNum)) {
			int showInt = Integer.parseInt(showNum);
			if(tempList.size() > showInt ) {
				resultList = tempList.subList(0, showInt);
			}else {
				resultList = tempList;
			}
		}else {
			resultList = tempList;
		}
		
		return ResultHelper.getResult(resultList);
	}

	
	/**
	 * 获取该部门以及下级部门警员分页信息
	 * @return ResultPojo 
	 */
	public ResultPojo getPersonInfoPage(String deptId, String personKey, Page page) {
		ResultPojo resultByDeptId = dubboPersonInfoService.getPagePersonInfoByParentDeptId(deptId, null, null, personKey, page);
		return resultByDeptId;
	}
	/**
	 * 获取该部门以及下级部门警员列表信息
	 * @return ResultPojo 
	 */
	public ResultPojo getPersonInfoList(String deptId, String personKey) {
		ResultPojo resultByDeptId = dubboPersonInfoService.getPersonInfoByParentId(deptId, null, null, personKey);
		return resultByDeptId;
	}
	
}
